package com.company.project.service.impl;
import com.company.project.vo.req.PhysicalExaminationIndexReq;

import com.alibaba.fastjson.JSON;
import com.company.project.common.config.HealthConfig;
import com.company.project.common.utils.Constant;
import com.company.project.entity.EJkzGrdjxxEntity;
import com.company.project.entity.EJkzRecordLogEntity;
import com.company.project.entity.EJkzTjxxEntity;
import com.company.project.service.EJkzRecordLogService;
import com.company.project.service.EJkzTjxxService;
import com.company.project.service.MedicalInformationTaskService;
import com.company.project.vo.req.PhysicalExaminationReq;
import com.company.project.vo.resp.HealthCertificateResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author liuhui
 * @Created 2020-11-17
 */
@Service
@Slf4j
public class MedicalInformationTaskServiceImpl implements MedicalInformationTaskService {
    private static final String INTERFACE_METHOD = "/api/exchange/tjxxRegister";
    @Resource
    private HealthConfig healthConfig;
    @Resource
    private EJkzTjxxService eJkzTjxxService;

    @Resource
    private EJkzRecordLogService eJkzRecordLogService;

    @Override
    public boolean uploadMedicalInformation(EJkzTjxxEntity entity,EJkzGrdjxxEntity grdjxxEntity) {
        PhysicalExaminationReq req = buildPhysicalExaminationReq(entity, grdjxxEntity);
        String result = sendMessage(JSON.toJSONString(req),healthConfig.getUrl()+INTERFACE_METHOD);
        HealthCertificateResult healthCertificateResult = JSON.parseObject(result, HealthCertificateResult.class);
        boolean uploadResult = healthCertificateResult.getSuccess();
        // 存储接口调用日志
        if (uploadResult) {
            entity.setUploadStatus(Constant.DATA_UPLOAD);
            eJkzTjxxService.saveOrUpdate(entity);
        }
        eJkzRecordLogService.save(buildRecordLog(entity, result, uploadResult));
        return uploadResult;
    }


    private EJkzRecordLogEntity buildRecordLog(EJkzTjxxEntity param, String result, boolean uploadResult) {
        EJkzRecordLogEntity logEntity = new EJkzRecordLogEntity();
        logEntity.setSclx(Constant.MEDICAL_INFORMATION_TYPE);
        logEntity.setWbId(param.getWbId());
        logEntity.setUploadStatus(uploadResult ? Constant.UPLOAD_STATUS_SUCCESS : Constant.UPLOAD_STATUS_FAIL);
        logEntity.setUploadResult(result);
        logEntity.setCreateTime(new Date());
        logEntity.setUpdateTime(new Date());
        logEntity.setDelFlag(Constant.DATA_NOT_DELETED_STRING_VALUE);
        return logEntity;
    }

    /**
     * 发送消息
     *
     * @param params
     * @param url
     * @return
     */
    private String sendMessage(String params, String url) {
        log.info("上传参数={}", params);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(params, headers);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.postForEntity(url, request, String.class).getBody();
        log.info("上传结果={}", result);
        return result;
    }

    private PhysicalExaminationReq buildPhysicalExaminationReq(EJkzTjxxEntity tjxx, EJkzGrdjxxEntity jbxx) {
        PhysicalExaminationReq req = new PhysicalExaminationReq();
        req.setUsername(healthConfig.getUserName());
        req.setPassword(healthConfig.getPassword());
        req.setBgk_lx(jbxx.getOperationType());
        req.setWbId(jbxx.getWbId());
        req.setName(jbxx.getName());
        req.setCardType(jbxx.getCardType());
        req.setIdCard(jbxx.getIdCard());
        req.setTjResult(tjxx.getExamineResult());
        req.setTjTime(tjxx.getExamineTime());
        req.setTjReportTime(tjxx.getReportTime());
        req.setTjDataSource(tjxx.getYljgCompName());
        req.setTjjgName(tjxx.getYljgCompName());
        PhysicalExaminationIndexReq indexReq =new PhysicalExaminationIndexReq();
        indexReq.setTjx001(tjxx.getTjx001());
        indexReq.setTjx002(tjxx.getTjx005());
        indexReq.setTjx003(tjxx.getTjx004());
        indexReq.setTjx004(tjxx.getTjx003());
        indexReq.setTjx005(tjxx.getTjx007());
        // 大便 无
        indexReq.setTjx009(tjxx.getTjx010());
        indexReq.setTjx010(tjxx.getTjx012());
        indexReq.setTjx011(tjxx.getTjx011());
        indexReq.setTjx012(tjxx.getTjx022());
        indexReq.setTjx018(tjxx.getTjx023());
        indexReq.setTjx019(tjxx.getTjx024());
        indexReq.setTjx024(tjxx.getTjx024());
        indexReq.setTjx025(tjxx.getTjx025());
        indexReq.setTjx036(tjxx.getTjx008());
        indexReq.setTjx037(tjxx.getTjx006());
        indexReq.setTjx02601(tjxx.getTjx01301());
        indexReq.setTjx02602(tjxx.getTjx01302());
        indexReq.setTjx02701(tjxx.getTjx01401());
        indexReq.setTjx02702(tjxx.getTjx01402());
        indexReq.setTjx02801(tjxx.getTjx01501());
        indexReq.setTjx02802(tjxx.getTjx01502());
        indexReq.setTjx02901(tjxx.getTjx01601());
        indexReq.setTjx02902(tjxx.getTjx01602());
        indexReq.setTjx03001(tjxx.getTjx01701());
        indexReq.setTjx03002(tjxx.getTjx01702());
        indexReq.setTjx03101(tjxx.getTjx01801());
        indexReq.setTjx03102(tjxx.getTjx01802());
        indexReq.setTjx03201(tjxx.getTjx01901());
        indexReq.setTjx03202(tjxx.getTjx01902());
        indexReq.setTjx03501(tjxx.getTjx02001());
        indexReq.setTjx03502(tjxx.getTjx02002());
        indexReq.setTjx036Code1(tjxx.getTjx008Code1());
        indexReq.setTjx036Code2(tjxx.getTjx008Code2());
        indexReq.setTjx036Code3(tjxx.getTjx008Code3());
        indexReq.setTjx036Code4(tjxx.getTjx008Code4());
        indexReq.setTjx036Code5(tjxx.getTjx008Code5());
        indexReq.setTjx036Code6(tjxx.getTjx008Code6());
        req.setTjItemJson(JSON.toJSONString(indexReq));
        return req;





    }
}
